package com.example.aacwebdemo.es2;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhao.xin06679
 * @date 2021年06月04日 11:01
 */
@RestController
@RequestMapping("/es1")
public class EsController1 {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    EsDaoUserAction esDaoUserAction;

    @RequestMapping(value="saveTest",method = RequestMethod.GET)
    public String saveTest(){

        IndexCoordinates index = IndexCoordinates.of("zhaozhao");
        EsBeanUserAction zz= EsBeanUserAction.build(1);

        elasticsearchRestTemplate.save(zz,index);

        zz= EsBeanUserAction.build(2);

        elasticsearchRestTemplate.save(zz,index);
        return "success";
    }

    @RequestMapping("saveActionOne")
    public String saveActionOne() {
        List<EsBeanUserAction> list = new ArrayList<>();

        //for (int i = 0 ; i < 100000; i++ ){
        for (int i = 0 ; i < 1; i++ ){
            list.add(EsBeanUserAction.build(i));

            if (list.size() > 10000){
                System.out.println(i);
                esDaoUserAction.saveAll(list);
                list.clear();
            }
        }

        if(list.size()>0){
            esDaoUserAction.saveAll(list);
            list.clear();
        }

        return "success";
    }

    public String updateActionOne(){
        List<EsBeanUserAction> list = new ArrayList<>();

        EsBeanUserAction a = EsBeanUserAction.build(5890601);
        a.setSectorID(new byte[]{Integer.513,662});

        esDaoUserAction.save(a);

        return "success";

    }


    @RequestMapping("saveActionData")
    public String saveActionData() {
        List<EsBeanUserAction> list = new ArrayList<>();

        //for (int i = 0 ; i < 100000; i++ ){
        for (int i = 0 ; i < 20000000; i++ ){
            list.add(EsBeanUserAction.build(i));

            if (list.size() > 10000){
                System.out.println(i);
                esDaoUserAction.saveAll(list);
                list.clear();
            }
        }

        if(list.size()>0){
            esDaoUserAction.saveAll(list);
            list.clear();
        }

        return "success";
    }

    @RequestMapping("findByID")
    public Object findByUserID(String EntID) {
        return esDaoUserAction.findById(0L);


//        ValueCountBuilder vcb=  AggregationBuilders.count("count_uid").field("uid");
//
//        TermsAggregationBuilder builder = AggregationBuilders.terms("count").field("entID");
//        //构建查询
//        NativeSearchQuery query = new NativeSearchQueryBuilder().addAggregation(builder).build();
//
//        Page page = esDao.search(query);
//
//        System.out.print(page.stream().toArray());


  //      return result;


    }

}
